package com.example.demo.generator.scaffold.model;

import java.util.Objects;
import java.util.function.Function;

/**
 * 定义单个模板的渲染信息。
 */
public class TemplateDescriptor {

    private final String id;
    private final String templatePath;
    private final Function<TemplateContext, String> outputPathResolver;

    private TemplateDescriptor(Builder builder) {
        this.id = builder.id;
        this.templatePath = builder.templatePath;
        this.outputPathResolver = builder.outputPathResolver;
    }

    public String getId() {
        return id;
    }

    public String getTemplatePath() {
        return templatePath;
    }

    public String resolveOutputPath(TemplateContext context) {
        return outputPathResolver.apply(context);
    }

    public static Builder builder() {
        return new Builder();
    }

    public static class Builder {
        private String id;
        private String templatePath;
        private Function<TemplateContext, String> outputPathResolver;

        public Builder id(String id) {
            this.id = id;
            return this;
        }

        public Builder templatePath(String templatePath) {
            this.templatePath = templatePath;
            return this;
        }

        public Builder outputPathResolver(Function<TemplateContext, String> resolver) {
            this.outputPathResolver = resolver;
            return this;
        }

        public TemplateDescriptor build() {
            Objects.requireNonNull(id, "id must not be null");
            Objects.requireNonNull(templatePath, "templatePath must not be null");
            Objects.requireNonNull(outputPathResolver, "outputPathResolver must not be null");
            return new TemplateDescriptor(this);
        }
    }
}
